package com.sunda.spmsweb.wmscontroller;


import com.alibaba.fastjson.JSONObject;
import com.sunda.spmscommon.ResponseResult;
import com.sunda.spmsweb.util.JWTUtil;
import com.sunda.spmswms.service.IInventoryCheckService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 仓库盘点任务表 前端控制器
 * </p>
 *
 * @author Wayne
 * @since 2021-05-10
 */
@RestController
@RequestMapping("/inventorycheck")
@Api(tags = "仓库盘点接口", description = "仓库盘点接口")
public class InventoryCheckController {

    @Autowired
    IInventoryCheckService iInventoryCheckService;

    @PostMapping("/getInventoryCheck")
    @ApiOperation(value = "获取库存盘点任务列表", notes = "获取库存盘点任务列表" +
            "{\n" +
            "  \"werks\":[\"CN01\",\"CM31\"],\n" +
            "  \"whsLocationCode\":[\"1011\",\"1061\"],\n" +
            "  \"inventoryCheckType\":\"X\",  // X-备件货, M-贸易货\n" +
            "  \"inventoryCheckCategory\":\"1,2\", //盘点类别 1-日常盘点 2-月度盘点 3-动态盘点 4-其它盘点, 多个类别以 , 分割\n" +
            "  \"inventoryCheckDate\":[\"20210401\",\"20210501\"],\n" +
            "  \"inventoryCheckResult\":\"1\", //盘点结果 1-账实相符 2-盘盈 3-盘亏 4-盘盈盘亏\n" +
            "  \"inventoryCheckId\":\"CN01-1061-20210706-007\",  //盘点单号 CN01-1061-20210706-006\n" +
            "  \"isReview\":\"1\", //是否复盘 1-初盘 2-复盘\n" +
            "  \"spmsStatus\":\"1,2\", //任务状态：1-待盘点；2-盘点中；3-复盘中；4-处理中；5-关闭(完成), 多个状态以 , 分割\n" +
            "}", httpMethod = "POST")
    public ResponseResult getInventoryCheck(@RequestBody JSONObject InvCheckFilter) {
        return iInventoryCheckService.getInventoryCheck(InvCheckFilter);
    }

    @GetMapping("/getInventoryCheckWithDtl")
    @ApiOperation(value = "根据uuid获取盘点单据详情数据", notes = "根据uuid获取盘点单据详情数据")
    public ResponseResult getInventoryCheckWithDtl(@RequestParam(required = true) String uuid) {
        return iInventoryCheckService.getInventoryCheckWithDtl(uuid);
    }

    @PostMapping("/updateInventoryCheck")
    @ApiOperation(value = "新建,更新盘点单", notes = "新建,更新盘点单" +
            "{\n" +
            "  \"inventoryCheck\": {\n" +
            "    \"uuid\": \"盘点单的唯一id\",\n" +
            "    \"inventoryCheckId\": \"盘点单号\",\n" +
            "    \"werks\": \"工厂\",\n" +
            "    \"whsLocationCode\": \"仓库\",\n" +
            "    \"inventoryCheckType\": \"是否杂货 X-备件货 M-贸易货\",\n" +
            "    \"inventoryCheckCategory\": \"盘点类别 1-日常盘点 2-月度盘点 3-动态盘点 4-其它盘点\",\n" +
            "    \"inventoryCheckMold\": \"盘点类型 1-明盘 2-暗盘\",\n" +
            "    \"inventoryCheckDate\": \"盘点日期\",\n" +
            "    \"inventoryCheckResult\": \"盘点结果 1-账实相符 2-盘盈 3-盘亏 4-盘盈盘亏\",\n" +
            "    \"isReview\": \"是否复盘 1-初盘 2-复盘\",\n" +
            "    \"inventoryReviewId\": \"复盘单据UUID\",\n" +
            "    \"inventoryCheckSurplus\": \"盘盈单据号\",\n" +
            "    \"inventoryCheckLoss\": \"盘亏单据号\",\n" +
            "    \"createDate\": \"创建日期\",\n" +
            "    \"spmsStatus\": \"任务状态：1-待盘点；2-盘点中；3-复盘中；4-处理中；5-关闭(完成)\",\n" +
            "    \"requester\": \"创建人工号\",\n" +
            "    \"operator\": \"盘点人\",\n" +
            "    \"reviewer\": \"复核人\",\n" +
            "    \"approver\": \"审批人\",\n" +
            "    \"createTime\": \"创建时间\",\n" +
            "    \"updateUsername\": \"更新人\",\n" +
            "    \"updateTime\": \"更新时间\"\n" +
            "  },\n" +
            "  \"inventoryCheckDtls\": [\n" +
            "    {\n" +
            "      \"uuid\": \"盘点任务UUID\",\n" +
            "      \"taskRowId\": \"盘点任务行号\",\n" +
            "      \"materialNo\": \"物料编号\",\n" +
            "      \"boxNote\": \"箱码\",\n" +
            "      \"inventoryQuantity\": \"库存数量\",\n" +
            "      \"checkQuantity\": \"实盘数量\",\n" +
            "      \"discrepancy\": \"差异数量\",\n" +
            "      \"basicUnit\": \"基本单位\",\n" +
            "      \"storageUuid\": \"储位UUID\",\n" +
            "      \"remarks\": \"备注\",\n" +
            "    }\n" +
            "  ]\n" +
            "}", httpMethod = "POST")
    public ResponseResult updateInventoryCheck(@RequestBody JSONObject InvCheckDoc) {
        String userId = JWTUtil.getUserId(SecurityUtils.getSubject().getPrincipal().toString());
        return iInventoryCheckService.updateInventoryCheck(InvCheckDoc, userId);
    }

    @PostMapping("/saveInventoryCheckRes")
    @ApiOperation(value ="保存盘点结果及盘盈，盘亏单据",notes ="保存盘点结果及盘盈，盘亏单据,盘盈亏单据为可选，如果无盘盈亏单据，则只保存盘点单结果 \n" +
    "{\n" +
            "  \"invChkSurplus\": {\n" +
            "    \"invCheckAc\": {\n" +
            "      \"uuid\": \"盘盈盘亏任务UUID\",\n" +
            "      \"spmsId\": \"盘盈盘亏单号\",\n" +
            "      \"inventoryActionType\": \"盘盈盘亏类别 701-盘盈 702-盘亏\",\n" +
            "      \"werks\": \"工厂\",\n" +
            "      \"whsLocationCode\": \"仓库\",\n" +
            "      \"inventoryCheckId\": \"盘点单号\",\n" +
            "      \"inventoryCheckResult\": \"盘点结果 1-账实相符 2-盘盈 3-盘亏 4-盘盈盘亏\",\n" +
            "      \"inventoryCheckType\": \"是否杂货 X-备件货 M-贸易货\",\n" +
            "      \"inventoryCheckCategory\": \"盘点类别 1-日常盘点 2-月度盘点 3-动态盘点 4-其它盘点\",\n" +
            "      \"inventoryCheckMold\": \"盘点类型 1-明盘 2-暗盘\",\n" +
            "      \"createDate\": \"创建日期\",\n" +
            "      \"spmsStatus\": \"任务状态：1-草稿;2-审批中；3-审批不通过；4-审批通过(待上传SAP)；5-关闭(完成)\",\n" +
            "      \"requester\": \"创建人工号\",\n" +
            "      \"operator\": \"盘点人\",\n" +
            "      \"reviewer\": \"复核人\",\n" +
            "      \"approver\": \"审批人\",\n" +
            "      \"createTime\": \"创建时间\",\n" +
            "      \"updateUsername\": \"更新人\",\n" +
            "      \"updateTime\": \"更新时间\",\n" +
            "      \"justification\": \"申请原因\",\n" +
            "      \"remarks\": \"备注\"\n" +
            "    },\n" +
            "    \"invCheckAcDtls\": [\n" +
            "      {\n" +
            "        \"uuid\": \"盘盈盘亏任务UUID\",\n" +
            "        \"taskRowId\": \"盘盈盘亏任务行号\",\n" +
            "        \"materialNo\": \"物料编号\",\n" +
            "        \"boxNote\": \"箱码\",\n" +
            "        \"inventoryQuantity\": \"库存数量\",\n" +
            "        \"checkQuantity\": \"实盘数量\",\n" +
            "        \"discrepancy\": \"差异数量\",\n" +
            "        \"basicUnit\": \"基本单位\",\n" +
            "        \"storageUuid\": \"储位UUID\",\n" +
            "        \"remarks\": \"备注\"\n" +
            "      }\n" +
            "    ]\n" +
            "  },\n" +
            "  \"invChkLoss\": {\n" +
            "    \"invCheckAc\": {}, // 与盘盈单相同\n" +
            "    \"invCheckAcDtls\": [] // 与盘盈单相同\n" +
            "  },\n" +
            "  \"inventoryCheck\": {\n" +
            "    \"uuid\": \"盘点单的唯一id\",\n" +
            "    \"inventoryCheckId\": \"盘点单号\",\n" +
            "    \"werks\": \"工厂\",\n" +
            "    \"whsLocationCode\": \"仓库\",\n" +
            "    \"inventoryCheckType\": \"是否杂货 X-备件货 M-贸易货\",\n" +
            "    \"inventoryCheckCategory\": \"盘点类别 1-日常盘点 2-月度盘点 3-动态盘点 4-其它盘点\",\n" +
            "    \"inventoryCheckMold\": \"盘点类型 1-明盘 2-暗盘\",\n" +
            "    \"inventoryCheckDate\": \"盘点日期\",\n" +
            "    \"inventoryCheckResult\": \"盘点结果 1-账实相符 2-盘盈 3-盘亏 4-盘盈盘亏\",\n" +
            "    \"isReview\": \"是否复盘 1-初盘 2-复盘\",\n" +
            "    \"inventoryReviewId\": \"复盘单据UUID\",\n" +
            "    \"inventoryCheckSurplus\": \"盘盈单据号\",\n" +
            "    \"inventoryCheckLoss\": \"盘亏单据号\",\n" +
            "    \"createDate\": \"创建日期\",\n" +
            "    \"spmsStatus\": \"任务状态：1-待盘点；2-盘点中；3-复盘中；4-处理中；5-关闭(完成)\",\n" +
            "    \"requester\": \"创建人工号\",\n" +
            "    \"operator\": \"盘点人\",\n" +
            "    \"reviewer\": \"复核人\",\n" +
            "    \"approver\": \"审批人\",\n" +
            "    \"createTime\": \"创建时间\",\n" +
            "    \"updateUsername\": \"更新人\",\n" +
            "    \"updateTime\": \"更新时间\"\n" +
            "  },\n" +
            "  \"inventoryCheckDtls\": [\n" +
            "    {\n" +
            "      \"uuid\": \"盘点任务UUID\",\n" +
            "      \"taskRowId\": \"盘点任务行号\",\n" +
            "      \"materialNo\": \"物料编号\",\n" +
            "      \"boxNote\": \"箱码\",\n" +
            "      \"inventoryQuantity\": \"库存数量\",\n" +
            "      \"checkQuantity\": \"实盘数量\",\n" +
            "      \"discrepancy\": \"差异数量\",\n" +
            "      \"basicUnit\": \"基本单位\",\n" +
            "      \"storageUuid\": \"储位UUID\",\n" +
            "      \"remarks\": \"备注\",\n" +
            "    }\n" +
            "  ]\n" +
            "}")
    public ResponseResult saveInventoryCheckRes(@RequestBody JSONObject invChkRes){
        String userId = JWTUtil.getUserId(SecurityUtils.getSubject().getPrincipal().toString());
        return iInventoryCheckService.saveInventoryCheckRes(invChkRes, userId);
    }

    @PostMapping("/getInventoryCheckLogs")
    @ApiOperation(value="获取盘点单操作日志",notes="获取盘点单操作日志\n" +
            "示例 uuid = 06d6b6e828014950b1a0feec5edca028 ", httpMethod = "POST")
    public ResponseResult getICALogs(String uuid){
        return ResponseResult.success().add("inventoryCheckActionLogs", iInventoryCheckService.getInventoryCheckLogs(uuid));
    }
}
